import { Injectable } from '@angular/core';
import * as dtos from './todo.model';
import { Http, Headers, RequestOptions, Request, Response, RequestMethod
          , ResponseOptions, ResponseType } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { UUID } from 'angular2-uuid';


@Injectable()
export class TodoService {
  private WebServerAddr = 'http://localhost:8010/json/reply/';
  //private WebServerAddr = '/json/reply/';

  constructor(private _http: Http) { }
  postTodo( todo: dtos.Todo ){
    let request: dtos.PostTodo = new dtos.PostTodo();
    request.Entity = todo;
    let headers = new Headers({'Content-Type': 'application/json'});
    let options = new RequestOptions({headers: headers});
    return this._http.post(this.WebServerAddr+"PostTodo", JSON.stringify(request))
      .map(value=>{
        let response: dtos.PostTodoResponse = value.json();
        return response.Id;
      });
  }
  getTodo(){
    let request: dtos.GetTodos = new dtos.GetTodos();
    request.Id = 0;
    request.Filter = "";
    let headers = new Headers({'Content-Type': 'application/json'});
    let options = new RequestOptions({headers: headers, params: request});
    return this._http.get(this.WebServerAddr+"GetTodos", options)
      .map(value=>{
        let response: dtos.GetTodosResponse = value.json();
        return response.Entities;
      });
  }
  getFilterTodo(filter: string){
    let request: dtos.GetTodos = new dtos.GetTodos();
    request.Id = 0;
    if(filter == 'ALL')
      request.Filter = "";
    else if(filter == 'ACTIVE')
      request.Filter = "Completed=0";
    else if(filter == 'COMPLETED')
      request.Filter = "Completed=1";
    else
      request.Filter = "";
    let headers = new Headers({'Content-Type': 'application/json'});
    let options = new RequestOptions({headers: headers, params: request});
    return this._http.get(this.WebServerAddr+"GetTodos", options)
      .map(value=>{
        let response: dtos.GetTodosResponse = value.json();
        return response.Entities;
      });
  }
  deleteTodoById(id: number){
    let request: dtos.DeleteTodo = new dtos.DeleteTodo();
    request.Id = id;
    let headers = new Headers({'Content-Type': 'application/json'});
    let options = new RequestOptions({headers: headers, params: request});
    return this._http.delete(this.WebServerAddr+"DeleteTodo", options)
      .map(value=>{
        let response: dtos.DeleteTodoResponse = value.json();
        return response;
      });
  }

  login(name: string, pass: string){
    let request = new dtos.UserLogin();
    request.UserName = name;
    request.PassWord = pass;
    request.MinutesOfTimeout = 30;
    return this._http.post(this.WebServerAddr+"UserLogin",  JSON.stringify(request))
      .map(value=>{
        let response: dtos.UserLoginResponse = value.json();
        if(response.SessionID != '')
        {
          return true;
        }else{
          return false;
        }
      });
  }
  logout(){
    let request = new dtos.UserLogout();
    request.SessionID = UUID.UUID();
    let headers = new Headers({'Content-Type': 'application/json'});
    let options = new RequestOptions({headers: headers, body: JSON.stringify(request)});
    return this._http.delete(this.WebServerAddr+"UserLogout", options)
      .map(value=>{
        let response: dtos.UserLogoutResponse = value.json();
        return true;
      });
  }
}
